import Mock from "mockjs";

const userList = [{ username: "admin", pwd: "123456" }];

export default {
  // 登录
  login: (config) => {
    let { username, pwd } = JSON.parse(config.body);
    let token = "";
    let list = userList.filter((el) => {
      return el.username == username;
    });
    if (list.length > 0) {
      if (username == list[0].username && pwd == list[0].pwd) {
        token = "admin---YWRtaW4=";
      } else {
        return {
          code: -1,
          msg: "用户名不存在或密码错误",
          data: { token },
        };
      }
      return {
        code: 200,
        msg: "登录成功",
        data: { token },
      };
    } else {
      return {
        code: -1,
        msg: "用户名不存在或密码错误",
        data: { token },
      };
    }
  },
  // 注册
  register: (config) => {
    let { username, pwd, checkpwd } = JSON.parse(config.body);
    for (let el of userList) {
      if (el.username == username) {
        return {
          code: 200,
          msg: "用户名已被注册",
          data: null,
        };
      } else {
        if (pwd != checkpwd) {
          return {
            code: 200,
            msg: "密码输入不一致",
            data: null,
          };
        } else {
          let reg =
            /^\S*(?=\S{6,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*? ])\S*$/;
          if (reg.test(pwd)) {
            userList.push({ username, pwd });
            return {
              code: 200,
              msg: "注册成功",
              data: null,
            };
          } else {
            return {
              code: 200,
              msg: "密码强度不够,请输入最少6位包含大小写字母、数字和特殊符号",
              data: null,
            };
          }
        }
      }
    }
  },
};
